﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using System.Reflection;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Biz;
using Cn.Edwon.Web;
using Cn.Cellula.Client;
using Cn.Cellula.Model.System;

namespace Cn.Cellula.Website.Admin.Item {
    public partial class EditAtl : PageSupport {
        IItemBiz biz = BizFactory.Get<IItemBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
            }
        }
        private void InitForm() {
            try {
                TItemEO ieo = biz.Load<TItemEO>(Request.QueryString["id"]);
                this.hid_ItemKey.Value = ieo.ItemKey;
                this.lab_ProductName.Text = ieo.ItemName;
                this.hid_CategoryKey.Value = ieo.CategoryKey;
                this.lab_CategoryName.Text = BizFactory.Get<ICategoryBiz>().LoadCategoryPath(this.OwnerKey, null, ieo.CategoryKey, " / ");
            } catch {
                SiteInfo.Send(Page, "错误", "没有指定有效的对象");
                this.btn_Save.Enabled = false;
                return;
            }
            this.lab_AtlForm.Text = ClientBiz.LoadItemAtlEditForm(this.hid_CategoryKey.Value, this.hid_ItemKey.Value,this.OwnerKey,this.Power);
        }

        protected void btn_Save_Click(object sender, EventArgs e) {
            IList<VSysAtlEO> list = biz.LoadSysAtlByCategory(this.hid_CategoryKey.Value, this.Power);
            IList<TItemAtlDataEO> dataList = new List<TItemAtlDataEO>();
            foreach (VSysAtlEO eo in list) {
                if (String.IsNullOrEmpty(Request.Form[eo.AtlKey])) continue;
                TItemAtlDataEO data = new TItemAtlDataEO();
                data.AtlKey = eo.AtlKey;
                data.DataValue = Request.Form[eo.AtlKey];
                if (eo.FieldType == CodeTypeDef.FormField.Item
                    || eo.FieldType == CodeTypeDef.FormField.User
                    || eo.FieldType == CodeTypeDef.FormField.Dept) {
                    data.RefItemKey = Request.Form[eo.AtlKey + "_ref"];
                }
                data.ItemKey = this.hid_ItemKey.Value;
                dataList.Add(data);
            }
            biz.SaveProductAtlData(dataList);
            Response.Redirect("ItemView.aspx?id="+this.hid_ItemKey.Value);
        }
    }
}